package com.sskj.lib.widget;

import android.app.Activity;
import android.graphics.Color;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.jakewharton.rxbinding2.widget.RxAdapterView;
import com.sskj.common.adapter.ItemClickSupport;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ImageUtil;
import com.sskj.common.util.SPUtil;
import com.sskj.lib.BaseHttpConfig;
import com.sskj.lib.Constans;
import com.sskj.lib.R;
import com.sskj.lib.RConfig;
import com.sskj.lib.SPConfig;
import com.sskj.lib.adapter.ArrowAdapter;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.util.CopyUtil;

import java.util.ArrayList;
import java.util.List;

public class BottomDialog {

    public static void showCountry(Activity activity, List<String> datas, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_country_dialog, null, false);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(activity)
                .setPaintColor(ContextCompat.getColor(activity, R.color.lib_divide))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(activity, 2))
                .setFirstDraw(false)
                .setLastDraw(false));
        SlimAdapter.create().register(R.layout.item_country_dialog, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector iViewInjector, List list) {
                iViewInjector.text(R.id.tv_country, data)
                        .clicked(R.id.ll_country, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onSure.onSure(data);
                                bottomSheetDialog.dismiss();
                            }
                        });
            }
        }).attachTo(recyclerView).updateData(datas);
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void changeLanguge(Activity activity, String grade, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_dialog_language, null, false);
        ClickUtil.click(300, inflate.findViewById(R.id.tv_cancel), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.tv_chinese), () -> {
            onSure.onSure(Constans.LANGUAGE_CNA);
            bottomSheetDialog.dismiss();
        });
        ClickUtil.click(300, inflate.findViewById(R.id.tv_traditional), () -> {
            onSure.onSure(Constans.LANGUAGE_TRA);
            bottomSheetDialog.dismiss();
        });
        ClickUtil.click(300, inflate.findViewById(R.id.tv_english), () -> {
            onSure.onSure(Constans.LANGUAGE_EN);
            bottomSheetDialog.dismiss();
        });

        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void posswordInput(Activity activity, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.shift_to_dialog, null, false);
        ClickUtil.click(300, inflate.findViewById(R.id.shift_dialog_cancel), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.shift_dialog_sure), () -> {
            EditText editText = inflate.findViewById(R.id.shift_dialog_password);
            onSure.onSure(editText.getText().toString());
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void Authentication(Activity activity, int cetification) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.authentication_item, null, false);
        TextView tv_msg = inflate.findViewById(R.id.tv_msg);
        if (cetification == 1) {
            tv_msg.setText(App.INSTANCE.getString(R.string.lib_bottomDialog1));
        } else if (cetification == 4) {
            tv_msg.setText(App.INSTANCE.getString(R.string.lib_bottomDialog2));
        }
        ClickUtil.click(300, inflate.findViewById(R.id.shift_dialog_cancel), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.shift_dialog_sure), () -> {
            ARouter.getInstance().build(RConfig.APP_ACTIVITY_CERTIFICATION).navigation();
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void chooseCoins(Activity activity, List<String> coins, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_dialog_recycler, null, false);
        ClickUtil.click(300, inflate.findViewById(R.id.tv_cancel), bottomSheetDialog::dismiss);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(activity)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(activity, 1))
                .setPaintColor(Color.TRANSPARENT)
                .setFirstDraw(false)
                .setLastDraw(false));
        BaseAdapter<String> adapter = new BaseAdapter<String>(R.layout.lib_item_coin, coins, recyclerView, false) {
            @Override
            public void bind(ViewHolder holder, String item) {
                holder.setText(R.id.textView, item);
            }
        };
        adapter.setNewData(coins);
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener((recyclerView1, position, v) -> {
            onSure.onSure(coins.get(position));
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void share(Activity activity, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_share, null, false);
        ClickUtil.click(300, inflate.findViewById(R.id.tv_cancel), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.tv_wechat), () -> {
            onSure.onSure(Constans.SHARE_WECHAT);
            bottomSheetDialog.dismiss();
        });
        ClickUtil.click(300, inflate.findViewById(R.id.tv_qq), () -> {
            onSure.onSure(Constans.SHARE_QQ);
            bottomSheetDialog.dismiss();
        });
        ClickUtil.click(300, inflate.findViewById(R.id.tv_pengyouquan), () -> {
            onSure.onSure(Constans.SHARE_PENGYOUQUAN);
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void walletAddress(Activity activity, String coinType, String url, String qrc) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_dialog_wallet_address, null, false);
        TextView tv_title = inflate.findViewById(R.id.tv_title);
        TextView tv_coin_type = inflate.findViewById(R.id.tv_coin_type);
        TextView tv_coin_address = inflate.findViewById(R.id.tv_coin_address);
        ImageView iv_close = inflate.findViewById(R.id.iv_close);
        ImageView iv_coin = inflate.findViewById(R.id.iv_coin);
        ImageView iv_coin_qrcode = inflate.findViewById(R.id.iv_coin_qrcode);
        tv_title.setText("Your " + coinType + " Collection Address");
        tv_coin_type.setText(coinType);
        tv_coin_address.setText(url);
        ImageUtil.setImage(BaseHttpConfig.BASE_URL + qrc, iv_coin_qrcode);
        iv_coin.setImageResource(Constans.icons.get(coinType));
        ClickUtil.click(300, inflate.findViewById(R.id.iv_close), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.btn_copy), () -> {
            if (!TextUtils.isEmpty(tv_coin_address.getText().toString().trim())) {
                CopyUtil.copy(tv_coin_address.getText().toString().trim());
            } else {
                ToastUtils.show("Load collection address fail.");
            }
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void showDelimit(Activity activity, OnSureStrs onSureStrs) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.lib_bottom_dialog_delimit, null, false);
        bottomSheetDialog.setContentView(inflate);
        TextView tv_record = inflate.findViewById(R.id.tv_record);
        EditText transactionID = inflate.findViewById(R.id.et_id);
        Spinner spinner = inflate.findViewById(R.id.spinner);
        TextView tv_delimit_coin = inflate.findViewById(R.id.tv_delimit_coin);
        EditText amount = inflate.findViewById(R.id.et_amount);
        EditText passCode = inflate.findViewById(R.id.et_passcode);
        List<String> coins = new ArrayList<>();
        coins.add(Constans.COIN_BTC);
        coins.add(Constans.COIN_ETH);
        ArrowAdapter spinnerTypeAdapter = new ArrowAdapter(activity,
                R.layout.lib_item_spinner, coins);
        spinnerTypeAdapter.setDropDownViewResource(R.layout.lib_item_spinner_drop);
        spinner.setAdapter(spinnerTypeAdapter);
        spinner.setSelection(0);
        RxAdapterView.itemSelections(spinner)
                .subscribe(pos -> {
                    tv_delimit_coin.setText(coins.get(pos));
                });
        ClickUtil.click(300, tv_record, () -> {
            ARouter.getInstance().build(RConfig.WALLET_MXT_RECORD).withString(Constans.TYPE, Constans.MXT_DELIMIT).navigation();
            bottomSheetDialog.dismiss();
        });
        ClickUtil.click(300, inflate.findViewById(R.id.iv_close), bottomSheetDialog::dismiss);
        ClickUtil.click(300, inflate.findViewById(R.id.btn_delimit), () -> {
            if (TextUtils.isEmpty(transactionID.getText().toString().trim())) {
                ToastUtils.show(transactionID.getHint());
                return;
            }
            if (TextUtils.isEmpty(amount.getText().toString().trim())) {
                ToastUtils.show(amount.getHint());
                return;
            }
            if (TextUtils.isEmpty(passCode.getText().toString().trim())) {
                ToastUtils.show(passCode.getHint());
                return;
            }
            if (!passCode.getText().toString().trim().equals(SPUtil.get(SPConfig.PASSCODE, ""))) {
                ToastUtils.show("Passcode mistake.");
                return;
            }
            onSureStrs.onSure(transactionID.getText().toString().trim(),
                    amount.getText().toString().trim(),
                    Constans.coinIDs.get(tv_delimit_coin.getText().toString().trim()));
            bottomSheetDialog.dismiss();
        });
        try {
            // hack bg color of the BottomSheetDialog
            ViewGroup parent = (ViewGroup) inflate.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public interface OnSure {
        void onSure(String tv);
    }

    public interface OnSureStrs {
        void onSure(String... tv);
    }
//
//    public interface OnSureCharge {
//        void onSure(RechOrWithBean data);
//    }
//
//    public interface OnSureWallet {
//        void onSure(WalletTransferBean data);
//    }
//
//    public interface onSureTransfer {
//        void onSure(TransferInsidePreBean data);
//    }
//
//    public interface PathOnSure {
//        void onSure(WalletPathBean walletPathBean);
//
//    }
}
